<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 style="text-align: center; margin-bottom: 30px;">系统登录</h2>

      <el-form :model="loginForm" @submit.prevent="handleLogin">
        <el-form-item>
          <el-input
              v-model="loginForm.userName"
              placeholder="用户名"
              prefix-icon="User"
              size="large"
          />
        </el-form-item>

        <el-form-item>
          <el-input
              v-model="loginForm.pwd"
              placeholder="密码"
              prefix-icon="Lock"
              type="password"
              show-password
              size="large"
          />
        </el-form-item>
        <el-form-item>
          <el-radio-group v-model="loginForm.userType" style="width: 100%">
            <el-radio-button label="user" >普通用户</el-radio-button>
            <el-radio-button label="admin">管理员</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="rememberMe">记住我</el-checkbox>
        </el-form-item>
        <el-form-item style="margin-top: 30px;">
          <el-button
              type="primary"
              native-type="submit"
              style="width: 100%; height: 50px; font-size: 16px;"
              :loading="loading"
          >
            登录
          </el-button>
        </el-form-item>

        <el-form-item style="margin-top: 30px;">
          <el-button
              type="info"
              style="width: 100%; height: 50px; font-size: 16px;"
              @click="router.push('/yycsh/sign')"
          >
            注册
          </el-button>
        </el-form-item>
        <span @click="router.push('/yycsh/forget')" class="forget-password">忘记密码</span>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from "axios"
// 引入认证服务
import { AuthService } from '@/service/auth.ts'

const router = useRouter()

// 创建认证服务实例
const authService = new AuthService()

const loginForm = ref({
  id: '',
  userName: '',
  pwd: '',
  userType: "user"
})

// 添加记住我选项
const rememberMe = ref(false)
// 添加加载状态
const loading = ref(false)

const handleLogin = async () => {
  if (!loginForm.value.userName || !loginForm.value.pwd) {
    ElMessage.error('请输入用户名和密码')
    return
  }

  loading.value = true

  try {
    if(loginForm.value.userType === "user"){
      const {data} = await axios.get("/api/user", {params:loginForm.value});
      if(data.code===200){
        ElMessage.success('登录成功');

        // 使用 AuthService 存储认证信息
        // 假设返回的 data 包含 token 和用户信息
        authService.setAuth(data.token, {
          id: data.data.id,
          username: data.data.userName,
          userType: 'user'
        }, rememberMe.value);
        await router.push('/')

      }
      else{
        ElMessage.error('用户名或密码错误');
      }
    }
    else{
      // AdminLogin
      const {data} = await axios.get("/api/AdminLogin", {params:loginForm.value});
      if(data != null && data !== ''){
        ElMessage.success('登录成功');

        // 使用 AuthService 存储认证信息
        authService.setAuth(data.token, {
          id: data.id,
          username: data.yhm,
          userType: 'admin'
        }, rememberMe.value);

        await router.push('/admin')
      }
      else{
        ElMessage.error('用户名或密码错误');
      }
    }
  } catch (error) {
    ElMessage.error('登录失败，请稍后重试');
    console.error('登录错误:', error);
  } finally {
    loading.value = false;
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  position: fixed;
  right: 20px;
  align-items: center;
  min-height: 100vh;
  width: 500px;
}

.login-card {
  width: 400px;
  min-height: 350px;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.el-input {
  height: 50px;
  font-size: 16px;
}
.forget-password{
  cursor: pointer; /* 鼠标悬停时变为手形指针 */
  color: blue;
}
</style>
